package com.example.activity_service.dao;

import com.example.activity_service.vo.Red;

import org.apache.ibatis.annotations.*;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.example.activity_service.vo.Address;
import org.springframework.stereotype.Repository;

import javax.ws.rs.DELETE;
import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface Yssdao {
    @Select("select * from hongbao where status=1 and users=#{users}")
    List<Red>searchhb(int users);

    @Select("select * from hongbao where status=2 and users=#{users}")
    List<Red>searchsyhb(int users);

    @Select("select * from hongbao where status=3 and users=#{users}")
    List<Red>searchgqhb(int users);





    @Update("update hongbao set status=2 where id=#{id}")
    int updatezt(int id, int status);
    @Select("select * from hongbao where status=0 ")
    List<Red> cahwei();
    //根据时间比较改成过期红包
    @Update("update hongbao set status =3 where timedata<NOW()")
    int updateguoqi( );

    //活动次数
    @Select("select * from huo_ci where userid=#{userid} and huoid=#{nm}")
    String chacs(String userid,String nm);

    @Select("select * from address where user_id=#{user_id}")
    @Results({
            @Result(property = "addressid",column = "addressid"),
            @Result(property = "user_id" ,column = "user_id"),
            @Result(property = "addressname", column = "addressname"),
            @Result(property = "addressphone",column = "addressphone"),
            @Result(property = "addresscode",column = "addresscode"),
            @Result(property = "province",column = "province"),
            @Result(property = "scity",column = "scity"),
            @Result(property = "area",column = "area"),
            @Result(property = "town",column = "town"),
            @Result(property = "addressdetailed",column = "addressdetailed"),
            @Result(property = "status",column = "status"),


    })
    List<Address> showaddress(String user_id);

    //后台查所有红包
    @Select("select * from hongbao")
    List<Red>searchsuoyou();

    //后台删除红包
    @Delete("delete  from hongbao where id=#{id}")
    int deletehb(int id);

    //后台新增红包
    @Insert("insert into hongbao (preferentialway,parvalue,threshold,totalissued,users,collectiontime,timedata,status)values(#{preferentialway},#{parvalue},#{threshold},#{totalissued},#{users},NOW(),date_sub(NOW(),interval -30 day),'0') ")
    int tianjiahb(String preferentialway,String parvalue,String threshold,String totalissued,int users);

    //后台修改红包
    @Update("update hongbao set preferentialway=#{preferentialway},parvalue=#{parvalue},threshold=#{threshold},totalissued=#{totalissued} where id=#{id}")
    int updatehb(int id,int preferentialway,int parvalue,int threshold,int totalissued);
@Update("update hongbao set users=#{id} ,status=1 where id=#{juan}")
    void juan(String id, String juan);

@Insert("insert into huo_ci (userid ,huoid ,muns) values (#{id},#{nm},3)")
    int chahuo(String id,String nm);
@Update("update huo_ci set muns = IF(muns < 0, 0, muns -1) where  userid=#{id} and huoid=#{nm}")
    int xiuhuo(String id ,String nm);

//返回次数
    @Select("select muns from huo_ci where userid=#{id} and huoid=#{nm}")
    int fanhuics(String id,String nm);

}
